package com.example.springboot.themeleafdemo.demo;


import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;

@Controller
public class DemoController {


    @Value("classpath:static/table.json")
    private Resource areaRes;

    @RequestMapping("/helloHtml")
    public String helloHtml(Map<String, Object> map, HttpServletRequest request) {

        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(areaRes.getInputStream()));
            StringBuffer message=new StringBuffer();
            String line = null;
            while((line = br.readLine()) != null) {
                message.append(line);
            }
            String defaultString=message.toString();
            String result=defaultString.replace("\r\n", "").replaceAll(" +", "");
            System.out.println(result);
            Object parse = JSONObject.parse(result);
            map.put("obj",parse);
            request.getSession().setAttribute("haha","chen3");
            System.out.println(1);
        } catch (IOException e) {
            e.printStackTrace();
        }
        map.put("hello", "from TemplateController.helloHtml");
        return "helloHtml";
    }


    @RequestMapping({"/","index.html"})
    public String index(){
        return "index";
    }
}